<script setup lang="ts">
import { ref } from "vue";
import { onLoad ,onShow,onHide} from "@dcloudio/uni-app"
import { useAppStore } from '@/stores/appStore'
import { apiGetRemind,apiDeleteMention } from "../../apis/apis.js"
const appStore = useAppStore();
const mentionList = ref([
  {
    block: "A2",
    time: "2023-04-13 ", 
    content:"即将到来的天气将会影响您的地块"
  },
  {
    block: "A2",
    time: "2023-04-13 ", 
    content:"即将到来的天气将会影响您的地块"
  } 
])
//将时间字符串转换为时间，格式为xxxx-xx-xx 上午或者中午或者下午
const formatTime = (time: string) => {
  const date = new Date(time);
  // 获取年、月、日
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');
  
  // 获取小时（24小时制）
  const hours = date.getHours();
  
  // 根据小时确定上午、中午或下午
  let period = '';
  if (hours >= 5 && hours < 11) {
    period = '上午';
  } else if (hours >= 11 && hours < 13) {
    period = '中午';
  } else if (hours >= 13 && hours < 18) {
    period = '下午';
  } else {
    period = '晚上';
  }
  
  // 返回格式化后的字符串
  return `${year}-${month}-${day} ${period}`;
}

const noticeList = ref("");

const getRemind = async () => {
  try {
    const res = await apiGetRemind({uid:appStore.uid,deleteDelay:1})
    console.log(res.data);
    if(res.data.rows.length===0){
      mentionList.value = [{block:"",time:"",content:"暂无提醒"}]
    }else{
      //res返回的结果为数组，将数组中的内容为
      //block: "A2"
// content: "区块2的作物需要浇水了"
// createdAt: "2025-04-07T19:22:19.000Z"
// delay: false
// id: 100
// state: true
// time: "2025-04-10T19:21:44.000Z"
// uid: 1
// updatedAt: "2025-04-07T19:22:25.000Z"
//提取content,time,block,id
      let temp = res.data.rows.map((item:any) => ({content:item.content,time:item.time,block:item.block,id:item.id}));
      
      //将temp中的内容按照时间排序,最新的在前面
      temp.sort((a:any,b:any) => new Date(b.time).getTime() - new Date(a.time).getTime());
      //将值赋值给mentionList
      mentionList.value = temp;

      console.log(mentionList.value);
      // noticeList.value = res.data.rows;
    }

  } catch (error) {
    console.error(error);
  }
}


let itv: number

const iflog = ref(false);
const uid = ref(0);
onLoad(() => {
  iflog.value = appStore.isLoggedIn; 
  uid.value = appStore.uid;
})
//设置定时器，每2秒换mentionList的值作为noticeList的值
const counter = ref(0);
onShow(() => {
  getRemind()
  counter.value++;
  if(counter.value >= mentionList.value.length){
    counter.value = 0;
  }
  //处理mentionList.value[counter.value]的内容，将其转换为字符串，以block time content的形式，中间用空格隔开

  noticeList.value = mentionList.value[counter.value].block + " " + formatTime(mentionList.value[counter.value].time) + " " + mentionList.value[counter.value].content;
  console.log(noticeList.value);

   itv = setInterval(() => {
  counter.value++;
  if(counter.value >= mentionList.value.length){
    counter.value = 0;
  }
  //处理mentionList.value[counter.value]的内容，将其转换为字符串，以block time content的形式，中间用空格隔开
  noticeList.value = mentionList.value[counter.value].block + " " + formatTime(mentionList.value[counter.value].time) + " " + mentionList.value[counter.value].content;
}, 2000);
})
onHide(() => {
  clearInterval(itv);
})


const getMore = () => {
  //跳转页面
  uni.navigateTo({url:"/pages/RemindList/index"})
};
</script>

<template>
  <view class="NoticeContainer">
    <uni-notice-bar 
      v-if="iflog"
      show-get-more
    
      show-icon
      :text="noticeList"
      :scrollable="false"
      :single="true"
      color="#2c5530"
      background-color="#e8f5e9"
      moreColor="#5db524"
      @getmore="getMore"
    />
  </view>
</template>

<style scoped lang="scss">
.NoticeContainer {
  width: 100%;
  height: auto;
  border-radius: 15px;
}
</style>
